package com.yizisu.basemvvm.utils

import android.app.Activity
import android.app.Application
import android.content.ContextWrapper
import androidx.fragment.app.Fragment
import com.yizisu.basemvvm.app
import kotlin.math.min

/**
 * 若红楼梦空，亦初心不变
 * 作者：kotle
 * 包名：com.yizisu.basemvvm.utils
 * 时间：2019/1/17 23:27
 * 描述：屏幕适配,app的尺寸不做处理，不做适配，只对activity适配
 * 可以根据需要来觉得是否适配
 */
object ScreenAdaptation {
    private val appDm = app.resources.displayMetrics
    private val customDensity = appDm.density
    private val customFontScaleDensity = appDm.scaledDensity

    //屏幕适配适配后的密度因子
    val density by lazy {
        min(
            appDm.widthPixels.toFloat(),
            appDm.heightPixels.toFloat()
        ) / MvvmConf.SCREEN_ADAPTATION_UI_DP_WIDTH
    }

    //屏幕适配后的字体缩放因子
    //因为适配之后的字体总是偏大，所以设置0.96使他小一点
    val scaledDensity by lazy { density * (customFontScaleDensity / customDensity) * 0.96f }

    //屏幕适配后的Dpi
    val densityDpi by lazy { (160 * density).toInt() }

    fun setCustomDensity(activity: Activity?) {
        activity ?: return
        val activityDm = activity.resources.displayMetrics
        activityDm.density = density
        activityDm.scaledDensity = scaledDensity
        activityDm.densityDpi = densityDpi
    }
}